package com.bc.bcapi.service;

import com.bc.bccore.redis.RedisService;
import com.bc.bcmodel.consts.RedissCacheKey;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.math.BigInteger;

import com.bc.bcapi.dao.T_SysconfMapper;
import com.bc.bcmodel.domain.T_SysconfModel;

/**
 * @Name T_SysconfService * @Description TODO * @Author generation * @Date 2018/8/9 15:56:31 * @Version 1.0
 **/
@Service
public class T_SysconfService {
    @Resource
    T_SysconfMapper t_SysconfMapper;

    @Resource
    RedisService redisService;

    /**
     * 保存单条数据     *      * @return
     */

    public int insert(T_SysconfModel req) {
        return t_SysconfMapper.insert(req);
    }

    /**
     * 批量增加数据     *      * @return
     */

    public int insertBatch(List<T_SysconfModel> req) {
        return t_SysconfMapper.insertBatch(req);
    }

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */

    public T_SysconfModel findByNamedParam(T_SysconfModel req) {
        return t_SysconfMapper.findByNamedParam(req);
    }

    /**
     * 根据对象查询所有数据     *      * @return
     */

    public List<T_SysconfModel> getAllList(T_SysconfModel req) {
        return t_SysconfMapper.findByNamedParamList(req);
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */

    public PageInfo<T_SysconfModel> getList(T_SysconfModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_SysconfModel> list = t_SysconfMapper.findByNamedParamList(req);
        return new PageInfo<T_SysconfModel>(list);
    }

    /**
     * 更新指定对象     *      * @return
     */

    public int update(T_SysconfModel req) {
        return t_SysconfMapper.update(req);
    }

    /**
     * 删除指定数据     *      * @return
     */

    public int delete(BigInteger id) {
        return t_SysconfMapper.delete(id);
    }

    /**
     * 根据条件统计数据量     *      * @return
     */

    public long count(T_SysconfModel req) {
        return t_SysconfMapper.count(req);
    }

    /**
     * 购买ICO平台将冲取5%作为佣金
     */
    public BigDecimal getPlatformCommission() {
        try {
            String key = RedissCacheKey.Project_Platform_Commission;
            String money = redisService.get(key);
            if (money == null || money.isEmpty()) {
                T_SysconfModel m = new T_SysconfModel();
                m.setS_key("platform_commission");
                T_SysconfModel resultModel = this.findByNamedParam(m);
                money = resultModel.getS_value();
                if (money.isEmpty()) {
                    money = "5";
                }
                redisService.set(key, money);
            }
            return new BigDecimal(money);
        } catch (Exception e) {
            return new BigDecimal(5);
        }
    }

}